#include "dbconfxmlparser.hpp"
#include <iostream>

using namespace com_oocl_riley;

DbConfXmlParser::DbConfXmlParser(const string &fileName)
    :XmlParser(fileName),_content(nullptr)
{
    _content=new vector<PtrDbConfig>;
}
DbConfXmlParser::~DbConfXmlParser()
{
    for(vector<PtrDbConfig>::iterator it=_content->begin(); it!=_content->end(); ++it)
        delete(*it);
}
void *DbConfXmlParser::getContent()
{
    return _content;
}
void DbConfXmlParser::printAllContent()
{
    std::cout<<"dbconfigs"<<std::endl;
    for(vector<PtrDbConfig>::const_iterator it=_content->cbegin(); it!=_content->cend(); ++it)
    {
        std::cout<<"\tdbconfig"<<std::endl;
        std::cout<<"\t\tconnectstring "<<(*it)->_connectStr<<std::endl;
        std::cout<<"\t\tusername "<<(*it)->_username<<std::endl;
        std::cout<<"\t\tpassword "<<(*it)->_password<<std::endl;
        std::cout<<"\t\tenv "<<(*it)->_env<<std::endl;
        std::cout<<"\tdbconfig"<<std::endl;
    }
    std::cout<<"dbconfigs"<<std::endl;
}
void DbConfXmlParser::tranverse(xmlDocPtr doc,xmlNodePtr cur)
{

    if(xmlStrcasecmp(cur->name,CAST_TO_XMLCHAR"dbconfigs"))
    {
        xmlFreeDoc(doc);
        return;
    }
    xmlNodePtr p=cur->children;
    while(p!=NULL)
    {
        if(p->type==XML_ELEMENT_NODE)
        {
            const xmlChar *name=CAST_TO_XMLCHAR p->name;
            if(xmlStrcasecmp(name,CAST_TO_XMLCHAR "dbconfig")==0)
            {
                xmlNodePtr c=p->children;
                PtrDbConfig dbConfig=new DbConfig;
                dbConfig->_env=CAST_TO_CHAR xmlGetProp(p,CAST_TO_XMLCHAR "env");
                while(c!=NULL)
                {
                    if(c->type==XML_ELEMENT_NODE)
                    {
                        name=c->name;
                        if(xmlStrcasecmp(name,CAST_TO_XMLCHAR "connectstring")==0)
                        {
                            dbConfig->_connectStr=CAST_TO_CHAR xmlNodeGetContent(c);
                        }
                        else if(xmlStrcasecmp(name,CAST_TO_XMLCHAR "username")==0)
                        {
                            dbConfig->_username=CAST_TO_CHAR xmlNodeGetContent(c);
                        }
                        else if(xmlStrcasecmp(name,CAST_TO_XMLCHAR "password")==0)
                        {
                            dbConfig->_password=CAST_TO_CHAR xmlNodeGetContent(c);
                        }
                    }
                    c=c->next;
                }
                _content->push_back(dbConfig);
            }
        }
        p=p->next;
    }

}
